use umass_yale_modules_2011, clear

svyset [pw=v101]

* Generate indicator for whether Williams if Democrat/Republican
gen cand_party=0 if condition<3 | condition==6 | condition==7
recode cand_party .=1
label define party 0 "Republican" 1 "Democrat"
label values cand_party party

* Generate indicator for whether Williams pandered to Born Agains
gen bornagain_pander=1 if condition==1 | condition==3
recode bornagain_pander .=0

* Generate indicator for whether Williams pandered to Gun Owners
gen gunowner_pander=1 if condition==2 | condition==4
recode gunowner_pander .=0

* Generate indicator for whether Williams pandered to Latinos
gen latino_pander=1 if condition==7 | condition==8
recode latino_pander .=0

* Generate indicator for whether Williams made general appeal
gen nopander=1 if condition==5 | condition==6
recode nopander .=0


* Generate indicator for people answering "not sure" to the support question 
gen votemissing=1 if williams_vote==997
replace votemissing=0 if williams_vote<997

* Generate indicator for people answering "not sure" to the question about Williams' ideology
gen ideologymissing=1 if williams_ideology==8
replace ideologymissing=0 if williams_ideology<8

* Recode the dependent variable so that "not sure" is missing
recode williams_vote 997=.

* Recode the ideology variable so that "not sure" is missing
recode williams_ideology 8=.

* Create a variable for R's ideology
gen self_ideology=cc342A
replace self_ideology=cc342a if self_ideology==.
recode self_ideology 8=.

* Generate a variable capturing the difference between R's rating of Williams' ideology and R's
* own ideology
gen ideo_distance=(williams_ideology-self_ideology)

* Generate a variable for whether R is born again
recode v215 2=0, gen(bornagain)

* Generate a variable for whether R is Latino
gen latino=1 if v211==3 | v290==1
recode latino .=0


*** Generate estimates for bottom half of Figure 3 ***

* Democratic candidate (left side of figure)
svy: reg williams_vote latino_pander if cand_party==1 & latino==1 & gunowner_pander~=1 & bornagain_pander~=1
svy: reg williams_vote bornagain_pander if cand_party ==1 & bornagain==1 & gunowner_pander~=1 & latino_pander~=1

* Republican candidate (left side of figure)
svy: reg williams_vote latino_pander if cand_party ==0 & latino==1 & gunowner_pander~=1 & bornagain_pander~=1
svy: reg williams_vote bornagain_pander if cand_party ==0 & bornagain==1 & gunowner_pander~=1 & latino_pander~=1


*** Generate estimates for bottom half of Figure 4 ***

* Democratic candidate (right side of figure)
svy: reg williams_vote latino_pander if cand_party ==1 & latino==0 & gunowner_pander~=1 & bornagain_pander~=1
svy: reg williams_vote bornagain_pander if cand_party ==1 & bornagain==0 & gunowner_pander~=1 & latino_pander~=1

* Republican candidate (right side of figure)
svy: reg williams_vote latino_pander if cand_party ==0 & latino==0 & gunowner_pander~=1 & bornagain_pander~=1
svy: reg williams_vote bornagain_pander if cand_party ==0 & bornagain==0 & gunowner_pander~=1 & latino_pander~=1


*** Generate estimates for Figure 5 ***

* Left panel of figure
svy: mean williams_vote if latino_pander==1, over(lf_latinos)
svy: mean williams_vote if nopander==1, over(lf_latinos)

* Middle panel of figure
svy: mean williams_vote if gunowner_pander==1, over(lf_gunowners)
svy: mean williams_vote if nopander==1, over(lf_gunowners)

* Right panel of figure
svy: mean williams_vote if bornagain_pander==1, over(lf_bornagain)
svy: mean williams_vote if nopander==1, over(lf_bornagain)

*** Generate estimates for Figure 6 ***
svy: mean williams_ideology if nopander==1, over(cand_party self_ideology)


*** Generate estimates for Figure 7 ***

* Top left panel of figure
svy: mean ideo_distance if latino_pander==0 & gunowner_pander==0 & bornagain==1 & self_ideology~=., over(cand_party bornagain_pander)

* Top right panel of figure
svy: mean ideo_distance if bornagain_pander==0 & gunowner_pander==0 & latino==1 & self_ideology~=., over(cand_party latino_pander)

* Bottom left panel of figure
svy: mean ideo_distance if latino_pander==0 & gunowner_pander==0 & bornagain==0 & self_ideology~=., over(cand_party bornagain_pander)

* Bottom right panel of figure
svy: mean ideo_distance if bornagain_pander==0 & gunowner_pander==0 & latino==0 & self_ideology~=., over(cand_party latino_pander)


